package com.example.bundledemoupdate;

import java.io.IOException;
import java.util.List;
import org.apkplug.Bundle.BundleActivityInit;
import org.osgi.framework.BundleException;
import org.osgi.framework.ServiceReference;

import com.apkplug.service.SearchApp.appBean;
import com.apkplug.service.download.AppDownload;
import com.apkplug.service.download.AppDownloadCallBack;
import com.apkplug.service.update.*;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	 Button button1=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1=(Button)this.findViewById(R.id.button1);
		button1.setOnClickListener(
				new OnClickListener(){
					public void onClick(View v) {
						// TODO Auto-generated method stub
						List<updateAppInfo> l=new java.util.ArrayList<updateAppInfo>();
						updateAppInfo info=new updateAppInfo();
						info.appid=BundleContextFactory.getInstance().getBundleContext().getBundle().getSymbolicName();
						info.bundlevarsion=BundleContextFactory.getInstance().getBundleContext().getBundle().getVersion();
						info.versionCode=getversionCode();
						l.add(info);
						updateAppBean bean=new updateAppBean();
						bean.setApps(l);
						checkupdate(bean);
					}
		});
	}

	/**
	 * 获取android程序的版本 
	 * 如果失败返回-1
	 * @return
	 */
	public int getversionCode(){

		PackageInfo info;
		try {
			info = MainActivity.this.getPackageManager().getPackageInfo( MainActivity.this.getPackageName(), 0);
			int versionCode=info.versionCode;
			return versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return -1;
		}  
		
	}
	
	public void checkupdate(updateAppBean bean){
		ServiceReference reference=BundleContextFactory.getInstance().getBundleContext()
        		.getServiceReference(checkupdate.class.getName());
    	if(null!=reference){
    		checkupdate service=(checkupdate) BundleContextFactory.getInstance().getBundleContext()
    				.getService(reference);
    		if(service!=null){
    			
    				service.checkupdate(bean, new updateCallBack(){

						@Override
						public void onFailure(int arg0, String arg1) {
							System.out.println(arg1);
							
						}

						@Override
						public void onSuccess(int arg0, final List<appBean> arg1,
								String arg2) {
							// TODO Auto-generated method stub
							if(arg0==updateCallBack.suc&&arg1.size()>0){
								button1.post(new Runnable(){
			    					 public void run(){
			    						 AlertDialog.Builder alertbBuilder = new AlertDialog.Builder(MainActivity.this);
											alertbBuilder
													.setTitle("有新版本是否更新？")
													.setMessage("你确定要更新？")
													.setPositiveButton("确定",
															new DialogInterface.OnClickListener() {
																public void onClick(DialogInterface dialog,
																		int which) {
																	download(arg1.get(0));
																	
																}
															})
													.setNegativeButton("取消",
															new DialogInterface.OnClickListener() {
																public void onClick(DialogInterface dialog,
																		int which) {
																	dialog.cancel();
																}
															}).create();
											alertbBuilder.show();
			    						 
			    						
			    					 }
			    				 });
								
							}else{
								Toast.makeText(MainActivity.this, "无更新", Toast.LENGTH_LONG).show();
							}
						}
    					
    				});
    		}
    		BundleContextFactory.getInstance().getBundleContext()
    		.ungetService(reference);
    	}
	}
	
	public void download( appBean ab){
    	ServiceReference reference=BundleContextFactory.getInstance().getBundleContext()
        		.getServiceReference(AppDownload.class.getName());
    	if(null!=reference){
    		AppDownload service=(AppDownload) BundleContextFactory.getInstance().getBundleContext()
    				.getService(reference);
				service.download(ab,MainActivity.this, 
						new AppDownloadCallBack(){
					@Override
					public void onDownLoadSuccess( String arg1) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onFailure(int arg0, String arg1) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onInstallSuccess(int stutas,final org.osgi.framework.Bundle arg0) {
						button1.post(new Runnable(){
	    					 public void run(){
	    						 AlertDialog.Builder alertbBuilder = new AlertDialog.Builder(MainActivity.this);
									alertbBuilder
											.setTitle("插件更新成功!")
											.setMessage("重新进入程序生效？")
											.setPositiveButton("确定",
													new DialogInterface.OnClickListener() {
														public void onClick(DialogInterface dialog,
																int which) {
															try {
																arg0.start();
															} catch (BundleException e) {
																// TODO Auto-generated catch block
																e.printStackTrace();
															}
															MainActivity.this.finish();
															
														}
													})
											.setNegativeButton("取消",
													new DialogInterface.OnClickListener() {
														public void onClick(DialogInterface dialog,
																int which) {
															dialog.cancel();
														}
													}).create();
									alertbBuilder.show();
	    						 
	    						
	    					 }
	    				 });
						
					}


					@Override
					public void onProgress(int arg0, int arg1, String arg2) {
						// TODO 自动生成的方法存根
						
					}
					
				});
    	}
	}

}
